<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
class X_Array
{
	/**
	 * Remove the specified element from an array
	 * return the value
     *
     * @param array $aArray
     * @param mixed $sKey
     * @return mixed
     */
	public static function popKey(Array &$aArray, $sKey)
	{
		if (array_key_exists($sKey, $aArray))
		{
			$xReturn = $aArray[$sKey];
			unset($aArray[$sKey]);
			return $xReturn;
		}

		return false;
	}
	
	/**
     * make an array into human readable PHP to aid code generation
     * Note: not the same as serializing
     * 	Also does not handle complex objects
     * 
     * @param $a
     */
    static public function toSource(array $a, $sIndent = null)
    {
    	// make sure we dont get in an infinate loop
    	static $iDepth = 0;
    	$iDepth++;
    	if ($iDepth > 5) return 'value beyond depth restrictions';
    	
    	$cEOL = "\n";
    	
    	$sReturn = 'array(' . $cEOL;
    	$sValues = null;
    	foreach ($a as $sKey => $sValue)
    	{
    		if (strlen($sValues)) $sValues .=  $cEOL . ',';
    		if (is_object($sValue))
    		{
    			// @todo make an interface for this
    			if (method_exists($sValue, 'toSource'))
    			{
    				$sValue = $sValue->toSource();
    			}
    			else
    			{
    				$sValue = (array)$sValue;
    			}
    		} 
    		
    		if (is_array($sValue))
    		{
    			$sValues .= '"' . $sKey . '"' . ' => ' . self::toSource($sValue) ;
    		}
    		else if (is_integer($sValue))
    		{
    			$sValues .= '"' . $sKey . '"' . ' => ' . $sValue;
    		}
    		else 
    		{
    			$sValues .= '"' . $sKey . '"' . ' => "' . (string)$sValue . '"';    			
    		}
    		
    	}
    	$sReturn .= $sValues.')';
    	
    	$iDepth--;
    	
    	return $sReturn;
    }	
}